package com.web.dao;

import com.web.entity.Account;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author FDjavaone
 */
public interface AccountMapper {

    @Delete({
        "delete from t_account",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_account (m_id, profit,",
        " consumption, merchant_payment,",
        " original_integral, core_points,",
        " msk, create_time, ",
        " modify_time) ",
        " values (#{mId,jdbcType=BIGINT}, #{profit,jdbcType=DECIMAL}, ",
        " #{consumption,jdbcType=DECIMAL}, #{merchantPayment,jdbcType=DECIMAL}, ",
        " #{originalIntegral,jdbcType=DECIMAL}, #{corePoints,jdbcType=DECIMAL}, ",
        " #{msk,jdbcType=DECIMAL}, #{createTime,jdbcType=TIMESTAMP}, ",
        " #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(Account record);

    @InsertProvider(type=AccountSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(Account record);



    @Select({
        "select",
        " id, m_id, profit, consumption, merchant_payment, original_integral, core_points, ",
        " msk, create_time, modify_time",
        " from t_account ",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
        @Result(column="profit", property="profit", jdbcType=JdbcType.DECIMAL),
        @Result(column="consumption", property="consumption", jdbcType=JdbcType.DECIMAL),
        @Result(column="merchant_payment", property="merchantPayment", jdbcType=JdbcType.DECIMAL),
        @Result(column="original_integral", property="originalIntegral", jdbcType=JdbcType.DECIMAL),
        @Result(column="core_points", property="corePoints", jdbcType=JdbcType.DECIMAL),
        @Result(column="msk", property="msk", jdbcType=JdbcType.DECIMAL),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Account selectByPrimaryKey(Long id);


    @UpdateProvider(type=AccountSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Account record);

    @Update({
        "update t_account",
        " set m_id = #{mId,jdbcType=BIGINT},",
          " profit = #{profit,jdbcType=DECIMAL},",
          " consumption = #{consumption,jdbcType=DECIMAL},",
          " merchant_payment = #{merchantPayment,jdbcType=DECIMAL},",
          " original_integral = #{originalIntegral,jdbcType=DECIMAL},",
          " core_points = #{corePoints,jdbcType=DECIMAL},",
          " msk = #{msk,jdbcType=DECIMAL},",
          " create_time = #{createTime,jdbcType=TIMESTAMP},",
          " modify_time = #{modifyTime,jdbcType=TIMESTAMP} ",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Account record);

    @Select({
            "select",
            " id, m_id, profit, consumption, merchant_payment, original_integral, core_points, ",
            " msk, create_time, modify_time ",
            " from t_account ",
            " where m_id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
            @Result(column="profit", property="profit", jdbcType=JdbcType.DECIMAL),
            @Result(column="consumption", property="consumption", jdbcType=JdbcType.DECIMAL),
            @Result(column="merchant_payment", property="merchantPayment", jdbcType=JdbcType.DECIMAL),
            @Result(column="original_integral", property="originalIntegral", jdbcType=JdbcType.DECIMAL),
            @Result(column="core_points", property="corePoints", jdbcType=JdbcType.DECIMAL),
            @Result(column="msk", property="msk", jdbcType=JdbcType.DECIMAL),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Account selectByMid(Long id);


    @Update({
            "update t_account",
            " set ",
            " profit =profit-#{totalPrice,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void profitSubtract(@Param("totalPrice") BigDecimal totalPrice, @Param("id") Long id);

    @Update({
            "update t_account",
            " set ",
            " profit =profit+#{totalPrice,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void profitAdd(@Param("totalPrice") BigDecimal totalPrice, @Param("id") Long id);

    @Update({
            "update t_account",
            " set ",
            " core_points =core_points+#{multiply,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void corePointsAdd( @Param("multiply") BigDecimal multiply, @Param("id")  Long id);


    @Update({
            "update t_account",
            " set ",
            " merchant_payment =merchant_payment+#{multiply,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void merchantPaymentAdd(@Param("multiply") BigDecimal multiply, @Param("id")  Long id);

    @Update({
            "update t_account",
            " set",
            " original_integral =original_integral-#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void originalSubtract( @Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);

    @Update({
            "update t_account",
            " set ",
            " consumption =consumption+#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void consumptionAdd( @Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);

    @Update({
            "update t_account",
            " set ",
            " consumption =consumption-#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void consumptionSubtract(@Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);


    @Update({
            "update t_account",
            " set ",
            " merchant_payment =merchant_payment-#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void merchantPaymentSubtract(@Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);


    @Update({
            "update t_account",
            " set ",
            " core_points =core_points-#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void corePointsSubtract(@Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);


    @Update({
            "update t_account",
            " set ",
            " msk =msk-#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void mskSubtract(@Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);

    @Select({
            "select",
            " id, m_id, profit, consumption, merchant_payment, original_integral, core_points, ",
            " msk, create_time, modify_time ",
            " from t_account ",
            " where msk != 0"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
            @Result(column="profit", property="profit", jdbcType=JdbcType.DECIMAL),
            @Result(column="consumption", property="consumption", jdbcType=JdbcType.DECIMAL),
            @Result(column="merchant_payment", property="merchantPayment", jdbcType=JdbcType.DECIMAL),
            @Result(column="original_integral", property="originalIntegral", jdbcType=JdbcType.DECIMAL),
            @Result(column="core_points", property="corePoints", jdbcType=JdbcType.DECIMAL),
            @Result(column="msk", property="msk", jdbcType=JdbcType.DECIMAL),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Account> findMskNotZero();

    @Update({
            "update t_account",
            " set ",
            " msk =msk+#{newNumber,jdbcType=INTEGER} ",
            " where m_id = #{id,jdbcType=BIGINT}"
    })
    void mskAdd(@Param("newNumber") int newNumber,  @Param("id") Long id);

    @Select({
            "select sum(merchant_payment) from t_account"
    })
    BigDecimal countMerchantNum();

    @Select({
            "select sum(core_points) from t_account"
    })
    BigDecimal countCoreNum();


    @Select({
            "select sum(consumption) from t_account"
    })
    BigDecimal countConsumeNum();

    @Select({
            "select sum(original_integral) from t_account"
    })
    BigDecimal countOriNum();

    @Select({
            "select sum(profit) from t_account"
    })
    BigDecimal countProfitNum();

    @Update({
            "update t_account",
            " set ",
            " original_integral =original_integral+#{newNumber,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void originalAdd( @Param("newNumber") BigDecimal newNumber,  @Param("id") Long id);

}